package config

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/config"
	configReq "gitcode.com/G-YT/iHealth25/server/model/config/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ConfigApi struct {
}

var appConfigService = service.ServiceGroupApp.ConfigServiceGroup.ConfigService

// CreateConfig 创建Config
// @Tags Config
// @Summary 创建Config
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body config.Config true "创建Config"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /appConfig/createConfig [post]
func (appConfigApi *ConfigApi) CreateConfig(c *gin.Context) {
	var appConfig config.Config
	err := c.ShouldBindJSON(&appConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":    {utils.NotEmpty()},
		"Value":  {utils.NotEmpty()},
		"Remark": {utils.NotEmpty()},
	}
	if err := utils.Verify(appConfig, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := appConfigService.CreateConfig(&appConfig); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteConfig 删除Config
// @Tags Config
// @Summary 删除Config
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body config.Config true "删除Config"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /appConfig/deleteConfig [delete]
func (appConfigApi *ConfigApi) DeleteConfig(c *gin.Context) {
	var appConfig config.Config
	err := c.ShouldBindJSON(&appConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := appConfigService.DeleteConfig(appConfig); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteConfigByIds 批量删除Config
// @Tags Config
// @Summary 批量删除Config
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Config"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /appConfig/deleteConfigByIds [delete]
func (appConfigApi *ConfigApi) DeleteConfigByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := appConfigService.DeleteConfigByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateConfig 更新Config
// @Tags Config
// @Summary 更新Config
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body config.Config true "更新Config"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /appConfig/updateConfig [put]
func (appConfigApi *ConfigApi) UpdateConfig(c *gin.Context) {
	var appConfig config.Config
	err := c.ShouldBindJSON(&appConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":    {utils.NotEmpty()},
		"Value":  {utils.NotEmpty()},
		"Remark": {utils.NotEmpty()},
	}
	if err := utils.Verify(appConfig, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := appConfigService.UpdateConfig(appConfig); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindConfig 用id查询Config
// @Tags Config
// @Summary 用id查询Config
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query config.Config true "用id查询Config"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /appConfig/findConfig [get]
func (appConfigApi *ConfigApi) FindConfig(c *gin.Context) {
	var appConfig config.Config
	err := c.ShouldBindQuery(&appConfig)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reappConfig, err := appConfigService.GetConfig(appConfig.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reappConfig": reappConfig}, c)
	}
}

// GetConfigList 分页获取Config列表
// @Tags Config
// @Summary 分页获取Config列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query configReq.ConfigSearch true "分页获取Config列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /appConfig/getConfigList [get]
func (appConfigApi *ConfigApi) GetConfigList(c *gin.Context) {
	var pageInfo configReq.ConfigSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := appConfigService.GetConfigInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetAllConfigList 获取全部系统配置列表
func (appConfigApi *ConfigApi) GetAllConfigList(c *gin.Context) {
	if list, err := appConfigService.GetAllConfigList(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(list, "获取成功", c)
	}
}
